package main;

import java.applet.Applet;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;

import javax.swing.ImageIcon;
import javax.swing.JApplet;

/**
 * Paddle class create Paddle.
 */
public class Paddle extends GameFrame{
    public Paddle(double x, double y, double w, double h) {
        super(x, y, w, h);
    }

    public void paint(Graphics g) {
        g.fillRect((int) brickRectangle2d.getX(),
                (int) brickRectangle2d.getY(), (int) brickRectangle2d
                        .getWidth(), (int) brickRectangle2d.getHeight());
    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        Point2D myPos = getPosition();
        Point2D crtSpeedPoint2d = speed;
        if (key == KeyEvent.VK_UP) {
            speed.setLocation(crtSpeedPoint2d.getX(), -50);
        }
        if (key == KeyEvent.VK_DOWN) {
            speed.setLocation(crtSpeedPoint2d.getX(), 50);
        }
        Point2D newPos = calculateNextPosition((float) 0.01);
        setPosition(newPos);
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        Point2D myPos = getPosition();
        Point2D crtSpeedPoint2d = speed;
        if (key == KeyEvent.VK_UP) {
            speed.setLocation(0, 0);
        }
        if (key == KeyEvent.VK_DOWN) {
            speed.setLocation(0, 0);
        }
    }

    public final boolean onHit(final ICollider hitBy) {
        Point2D myPos = getPosition();
        Point2D crtSpeedPoint2d = speed;
        if (hitBy.getMin().getX() < myPos.getX()
                && hitBy.getMax().getX() > myPos.getX()) {
            speed.setLocation(crtSpeedPoint2d.getX(), -crtSpeedPoint2d.getY());
        } else {
            speed.setLocation(-crtSpeedPoint2d.getX(), crtSpeedPoint2d.getY());
        }
        return true;
    }

}
